package proxy4jdbc3.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;
import proxy4jdbc3.JDBCProxy;
import proxy4jdbc3.JDBCReporter;

/**
 * Proxyfier a JDBC Connection and reports method calls, returns and exceptions.
 */
public class ConnectionProxy implements Connection, JDBCProxy<Connection> {
	
	private Connection realConnection;

	/*
	 * (non-javadoc)
	 */
	private JDBCReporter jdbcReporter;
	/**
	 * Create a new ConnectionProxy that wraps a given Connection.
	 * 
	 * @param jdbcReporter
	 * @param realConnection Connection that this ConnectionProxy wraps.
	 */
	public ConnectionProxy(JDBCReporter jdbcReporter, Connection realConnection) {
		// not initialized
		if (realConnection == null) {
			throw new IllegalArgumentException(
					"Must pass a non null real Connection");
		}
		
		if (jdbcReporter == null) {
			throw new IllegalArgumentException(
					"Must pass a non null jdbcReporter");
		}
		this.realConnection = realConnection;
		this.jdbcReporter = jdbcReporter;
	}

	public Connection getProxyfied() {		
		return realConnection;
	}


	public JDBCReporter getReporter() {
		return jdbcReporter;
	}
	
	// forwarding methods


	public boolean isClosed() throws SQLException {
		return getProxyfied().isClosed();
	}

	public SQLWarning getWarnings() throws SQLException {
		return (SQLWarning) getProxyfied().getWarnings();
	}

	public Savepoint setSavepoint() throws SQLException {
		return (Savepoint) getProxyfied().setSavepoint();
	}

	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		getProxyfied().releaseSavepoint(savepoint);
	}

	public void rollback(Savepoint savepoint) throws SQLException {
		getProxyfied().rollback(savepoint);
	}

	public DatabaseMetaData getMetaData() throws SQLException {
		return (DatabaseMetaData) getProxyfied().getMetaData();

	}

	public void clearWarnings() throws SQLException {
		getProxyfied().clearWarnings();
	}

	public Statement createStatement() throws SQLException {
		return new StatementProxy(this, getProxyfied().createStatement());
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		return new StatementProxy(this, getProxyfied().createStatement(
				resultSetType, resultSetConcurrency));
	}

	public Statement createStatement(int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		return new StatementProxy(this, getProxyfied().createStatement(
				resultSetType, resultSetConcurrency, resultSetHoldability));
	}

	public void setReadOnly(boolean readOnly) throws SQLException {
		getProxyfied().setReadOnly(readOnly);
	}

	public PreparedStatement prepareStatement(String sql) throws SQLException {
		return new PreparedStatementProxy(sql, this, getProxyfied()
				.prepareStatement(sql));
	}

	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException {
		return new PreparedStatementProxy(sql, this, getProxyfied()
				.prepareStatement(sql, autoGeneratedKeys));
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		return new PreparedStatementProxy(sql, this, getProxyfied()
				.prepareStatement(sql, resultSetType, resultSetConcurrency));
	}

	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		return new PreparedStatementProxy(sql, this, getProxyfied()
				.prepareStatement(sql, resultSetType, resultSetConcurrency,
						resultSetHoldability));
	}

	public PreparedStatement prepareStatement(String sql, int columnIndexes[])
			throws SQLException {
		return new PreparedStatementProxy(sql, this, getProxyfied()
				.prepareStatement(sql, columnIndexes));
	}

	public Savepoint setSavepoint(String name) throws SQLException {
		return getProxyfied().setSavepoint(name);
	}

	public PreparedStatement prepareStatement(String sql, String columnNames[])
			throws SQLException {
		return new PreparedStatementProxy(sql, this, getProxyfied()
				.prepareStatement(sql, columnNames));
	}

	public boolean isReadOnly() throws SQLException {
		return getProxyfied().isReadOnly();
	}

	public void setHoldability(int holdability) throws SQLException {
		getProxyfied().setHoldability(holdability);
	}

	public CallableStatement prepareCall(String sql) throws SQLException {
		return new CallableStatementProxy(sql, this, getProxyfied()
				.prepareCall(sql));
	}

	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		return new CallableStatementProxy(sql, this, getProxyfied()
				.prepareCall(sql, resultSetType, resultSetConcurrency));
	}

	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {

		return new CallableStatementProxy(sql, this, getProxyfied()
				.prepareCall(sql, resultSetType, resultSetConcurrency,
						resultSetHoldability));
	}

	public void setCatalog(String catalog) throws SQLException {
		getProxyfied().setCatalog(catalog);
	}

	public String nativeSQL(String sql) throws SQLException {
		return getProxyfied().nativeSQL(sql);
	}

	@SuppressWarnings("unchecked")
	public Map getTypeMap() throws SQLException {
		return getProxyfied().getTypeMap();
	}

	public void setAutoCommit(boolean autoCommit) throws SQLException {
		getProxyfied().setAutoCommit(autoCommit);
	}

	public String getCatalog() throws SQLException {
		return getProxyfied().getCatalog();
	}

	@SuppressWarnings("unchecked")
	public void setTypeMap(Map map) throws SQLException {
		getProxyfied().setTypeMap(map);
	}

	public void setTransactionIsolation(int level) throws SQLException {
		getProxyfied().setTransactionIsolation(level);
	}

	public boolean getAutoCommit() throws SQLException {
		return getProxyfied().getAutoCommit();
	}

	public int getHoldability() throws SQLException {
		return getProxyfied().getHoldability();
	}

	public int getTransactionIsolation() throws SQLException {
		return getProxyfied().getTransactionIsolation();
	}

	public void commit() throws SQLException {
		getProxyfied().commit();
	}

	public void rollback() throws SQLException {
		getProxyfied().rollback();
	}

	public void close() throws SQLException {
		getProxyfied().close();
	}
	
}